package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.service.AdWmSensitiveService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**敏感词用户层
 * @author lzq
 * @data 2023/5/9 11:51
 */
@RestController
@RequestMapping("/api/v1/sensitive")
public class AdWmSensitiveController {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private AdWmSensitiveService adWmSensitiveService;
    /**
     * 敏感词列表展示
     * @param dto
     * @return
     */
    @PostMapping("list")
    public ResponseResult list(@RequestBody SensitiveDto dto){
        return adWmSensitiveService.list(dto);
    }

    /**
     * 删除铭感词
     * @param id
     * @return
     */
    @DeleteMapping("/del/{id}")
    public ResponseResult del(@PathVariable Integer id){
        return adWmSensitiveService.del(id);
    }

    /**
     * 新增敏感词
     * @param wmSensitive
     * @return
     */
    @PostMapping("/save")
    public ResponseResult save(@RequestBody WmSensitive wmSensitive){
        //参数校验
        if (StringUtils.isBlank(wmSensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //保存
        wmSensitive.setCreatedTime(new Date());
        adWmSensitiveService.save(wmSensitive);
        //删除缓存敏感词数据
        redisTemplate.delete("wmSensitives");
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改敏感词
     * @param wmSensitive
     * @return
     */
    @PostMapping("/update")
    public ResponseResult update(@RequestBody WmSensitive wmSensitive){
        //参数校验
        if (wmSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //修改敏感词
         adWmSensitiveService.update(Wrappers.<WmSensitive>lambdaUpdate()
                 .eq(WmSensitive::getId,wmSensitive.getId())
                 .set(WmSensitive::getSensitives,wmSensitive.getSensitives())
                 .set(WmSensitive::getCreatedTime,new Date()));
        //删除缓存敏感词数据
        redisTemplate.delete("wmSensitives");
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
